package com.glory.wxelectricitypda.activity;


import android.os.Handler;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.base.BaseDialogActivity;
import com.glory.wxelectricitypda.bean.LotBean;
import com.glory.wxelectricitypda.bean.MaterialFixtureScrappedBean;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_TextCallback;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

public class MaterialFixtureScrappedActivity extends BaseDialogActivity {

    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.flEqpId)
    FrameLayout flEqpId;
    @BindView(R.id.tvEqpId)
    TextView tvEqpId;
    @BindView(R.id.ivEqpId)
    ImageView ivEqpId;
    @BindView(R.id.lotID)
    FreeUI_ClearEditText lotID;
    @BindView(R.id.scrappedNum)
    FreeUI_ClearEditText scrappedNum;
    @BindView(R.id.scrappedCause)
    FreeUI_ClearEditText scrappedCause;
    @BindView(R.id.scrappedRemark)
    FreeUI_ClearEditText scrappedRemark;
    private FreeUI_GeneralPop mPositionPop;


    private ArrayList<String> typeIDList = new ArrayList<>();
    private Map<String,String> typeIDMap = new HashMap<>();

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_material_fixture_scrapped;
    }

    @Override
    protected void initView() {
        freeUI_titleView.setTitle("物料/治具报废");
        freeUI_titleView.setRightListener("提交", new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (lotID.getText().toString().isEmpty()){
                    showToast("请扫描物料/治具批号！",false);
                    return;
                }
                if (scrappedNum.getText().toString().isEmpty()){
                    showToast("请输入报废数量！",false);
                    return;
                }
                submit();
            }
        });
    }

    @Override
    protected void initData() {
        getScrapcodeList();
        flEqpId.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mPositionPop != null){
                    if (mPositionPop.isShowing()) {
                        mPositionPop.dismiss();
                    }
                }else {
                    mPositionPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flEqpId.getWidth(), typeIDList, ivEqpId, new FreeUI_TextCallback() {
                        @Override
                        public void onSuccess(String s) {
                            tvEqpId.setText(s);
                        }
                    });
                }

                FreeApi_PopUtils.setArrowShow(ivEqpId);
                mPositionPop.showAsDropDown(flEqpId, 0, 0);
            }
        });
    }

    private void getScrapcodeList() {
        JSONObject map = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        try {
            map.put("ACTIONTYPE", "GETBYREFNAME");
            map.put("REFNAME", "MLotScrapCode");
            jsonObject.put("Data",map);
            jsonObject.put("MessageName","EUIT.EuitADUREFLISTHANDLER");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<LotBean>>(
                new TypeToken<ResponseBean<LotBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<LotBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    for (LotBean.AdurefBean adurefBean : response.body().getBody().getADUREFLIST()) {
                        typeIDList.add(adurefBean.getTEXT());
                        typeIDMap.put(adurefBean.getTEXT(),adurefBean.getKEYID());
                    }
                }
            }

            @Override
            public void onError(Response<ResponseBean<LotBean>> response) {
                super.onError(response);
                loadError(response.getException(), "ADUREFLISTHANDLER");
            }
        });

    }

    private void submit(){

        JSONObject map = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        try {
            map.put("ACTIONTYPE", "SCRAP");
            map.put("MLOTID", lotID.getText().toString());
            map.put("WAREHOUSEID", "");
            map.put("MAINQTY",scrappedNum.getText().toString());
            map.put("SUBQTY","0");
            map.put("ACTIONCODE",tvEqpId.getText().toString());
            map.put("ACTIONREASON",scrappedCause.getText().toString());
            map.put("ACTIONCOMMENT",scrappedRemark.getText().toString());
            map.put("COMMENT",scrappedRemark.getText().toString());
            jsonObject.put("Data",map);
            jsonObject.put("MessageName","EUIT.EuitScrapMLot");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<MaterialFixtureScrappedBean>>(
                new TypeToken<ResponseBean<MaterialFixtureScrappedBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<MaterialFixtureScrappedBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    showToast("报废成功", true);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            finish();
                        }
                    }, 2000);
                }
            }

            @Override
            public void onError(Response<ResponseBean<MaterialFixtureScrappedBean>> response) {
                super.onError(response);
                loadError(response.getException(), "EuitScrapMLot");
            }
        });



    }


    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return new FreeUI_AddViewEntity("header",llMain);
    }
}